﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;


namespace ERP_RORZE_SYSTEM
{
    public class INISettings
    {
        public string SQLConnectionString = "";
        public string DB2ConnectionString = "";
        public string HRConnectionString = "";
        private string encryptString = "Ktd123456";
        public string server = "";
        public string user = "";
        public string password = "";
        private string encryptPassword = "";
        public string database = "";
        public INISettings()
        {
            try
            {
                INIFile inif = new INIFile("RRCConfig.ini");
                server = inif.GetValue("DatabaseServer", "Server", "");
                user = inif.GetValue("DatabaseServer", "User", "");
                encryptPassword = inif.GetValue("DatabaseServer", "Password", "");
                password = inif.Decrypt(encryptPassword, encryptString);
                database = inif.GetValue("DatabaseServer", "Database", "");
                SQLConnectionString = "Data Source=" + server + ";Initial Catalog=" + database + ";User ID=" + user + ";Password=" + password;
                DB2ConnectionString = "Data Source=" + server + ";Initial Catalog=master;User ID=" + user + ";Password=" + password;
                HRConnectionString = "Data Source=" + server + ";Initial Catalog=master;User ID=" + user + ";Password=" + password;
            }
            catch //(Exception ex)
            {

            }
        }
        public bool CheckDatabaseConnection()
        {
            SQLConnectionString = "Data Source=" + server + ";Initial Catalog=" + database + ";User ID=" + user + ";Password=" + password;
            DB2ConnectionString = "Data Source=" + server + ";Initial Catalog=master;User ID=" + user + ";Password=" + password;
            HRConnectionString = "Data Source=" + server + ";Initial Catalog=master;User ID=" + user + ";Password=" + password;
            SqlConnection connection = new SqlConnection(SQLConnectionString);
            try
            {
                connection.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public void UpdateSetting()
        {
            SQLConnectionString = "Data Source=" + server + ";Initial Catalog=" + database + ";User ID=" + user + ";Password=" + password;
            DB2ConnectionString = "Data Source=" + server + ";Initial Catalog=master;User ID=" + user + ";Password=" + password;
            HRConnectionString = "Data Source=" + server + ";Initial Catalog=master;User ID=" + user + ";Password=" + password;

            INIFile inif = new INIFile("RRCConfig.ini");
            inif.SetValue("DatabaseServer", "Server", server);
            inif.SetValue("DatabaseServer", "User", user);
            encryptPassword = inif.Encrypt(password, encryptString);
            inif.SetValue("DatabaseServer", "Password", encryptPassword);
            inif.SetValue("DatabaseServer", "Database", database);

            inif.Flush();
        }
    }
}
